//
//  PCUserInfoController.m
//  PhotoCircle
//
//  Created by GL on 15/12/13.
//  Copyright © 2015年 wujungao. All rights reserved.
//

#import "PCUserInfoController.h"
#import "PCUserModel.h"
#import "PCMeStore.h"
#import "GMString.h"
#import "VPImageCropperViewController.h"
#import "UIImage+Convenience.h"
#import "GMTipsHelper.h"
#import "photoBtnDelegate.h"
#import "photoBtnView.h"

@interface PCUserInfoController ()<VPImageCropperDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,photoBtnDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *genderTextField;
@property (nonatomic, strong) PCUserModel *userModel;
@property (nonatomic, strong) photoBtnView *photoButtonView;

@end

@implementation PCUserInfoController

-(PCUserModel *)userModel{
    _userModel = [[PCUserInfoManager sharedUserInfo] readUserInfo];
    return _userModel;
}

-(void)configUI
{
    _nameTextField.text = self.userModel.userName;
    _genderTextField.text = self.userModel.userGender;
    _userImageView.image = [UIImage imageWithData:self.userModel.userImageData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView = [[UIView alloc]init];

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self configUI];

    MCHiddenBottomView();
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        // 头像#import "VPImageCropperViewController.h"
        [self resetHeadImage];
    }else if (indexPath.row == 1){
        // 姓名
        [self performSegueWithIdentifier:@"pushToModifyNameVC" sender:self];
    }else {
        // 性别
        [self performSegueWithIdentifier:@"pushToModifyGenderVC" sender:self];
    }
}

-(void)resetHeadImage
{
    UINib *nib = [UINib nibWithNibName:@"photoBtnView" bundle:[NSBundle mainBundle]];
    _photoButtonView = [[nib instantiateWithOwner:nil options:nil] firstObject];
    _photoButtonView.photoBtnDelegate = self;
    [self.navigationController.view addSubview:_photoButtonView];
}

-(void)photographUpload
{
    _photoButtonView.hidden = YES;
    [_photoButtonView removeFromSuperview];
    
    // 拍照
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    [self presentViewController:imagePicker animated:YES completion:^{
    }];

}

-(void)albumUpload
{
    _photoButtonView.hidden = YES;
    [_photoButtonView removeFromSuperview];
    
    // 相册
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:imagePicker animated:YES completion:^{
    }];
}

-(void)cancelAction
{
    _photoButtonView.hidden = YES;
    [_photoButtonView removeFromSuperview];
}

#pragma mark - ImagePickerController Delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^() {
        
        UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        portraitImg = [portraitImg imageByScalingToMaxSize:portraitImg];
        
        // present the cropper view controller
        VPImageCropperViewController *imgCropperVC = [[VPImageCropperViewController alloc] initWithImage:portraitImg cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width, self.view.frame.size.width) limitScaleRatio:3.0];
        imgCropperVC.delegate = self;
        [self presentViewController:imgCropperVC animated:YES completion:^{
            // TO DO
        }];
    }];
}

#pragma mark VPImageCropperDelegate
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {
    
    //[self.roundView updateWithImage:editedImage animated:YES];
    
    // 更新头像图片
    //存图片，更新model
    NSData *imgData = UIImageJPEGRepresentation(editedImage, 0.2);
    NSDictionary *dictionary = @{@"token":self.userModel.userToken};
    [GMTipsHelper showLoading];
    [[PCMeStore sharedMeStore] modifyUserHeader:dictionary image:imgData successHandler:^(NSInteger responCode){
        if (responCode == API_RESPONSE_OK) {
            [GMTipsHelper showSuccess:@"上传成功"];
            _userImageView.image = [UIImage imageWithData:imgData];
            self.userModel.userImageData = imgData;
            [[PCUserInfoManager sharedUserInfo] saveUserInfo:self.userModel];
        }else{
            [GMTipsHelper showFail:@"上传失败"];
        }
    } errorHandler:^{
        [GMTipsHelper showBadNetwork];
    }];
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
    }];
}

// 取消头像
- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
    }];
}
#pragma mark -
#pragma mark -ButtonEvent
- (IBAction)backEvent:(UIButton *)sender {
    [GMTipsHelper showDismiss];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
